package com.ht.web360.PojServer.controller;

import cn.hutool.core.util.NumberUtil;
import com.ht.web360.PojServer.dao.ObjRef;
import com.ht.web360.PojServer.service.ObjRefService;
import com.ht.web360.base.dto.PojInfo.dto.ObjRefDto;
import com.ht.web360.base.dto.Res;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/obj")
@Tag(name = "对象引用控制器")
public class ObjRefController {
    private final ObjRefService objRefService;

    public ObjRefController(ObjRefService objRefService) {
        this.objRefService = objRefService;
    }

    @Operation(summary = "通过项目id查找所有对象引用")
    @GetMapping("/{pojId}")
    public Res<List<ObjRefDto>> listRes(@PathVariable String pojId) {
        if (NumberUtil.isLong(pojId)) {
            throw new IllegalArgumentException();
        }
        List<ObjRef> objRefs = objRefService.refByPojId(Long.parseLong(pojId));
        List<ObjRefDto> dtoList = objRefs.stream().map(ObjRef::toDto).toList();
        return Res.ok(dtoList);
    }

    @Operation(summary = "通过对象id查找所有对象引用")
    @GetMapping("/byObj/{objId}")
    public Res<List<ObjRefDto>> listResByObjId(@PathVariable String objId) {
        if (NumberUtil.isLong(objId)) {
            throw new IllegalArgumentException();
        }
        List<ObjRef> objRefs = objRefService.refByObjId(Long.parseLong(objId));
        List<ObjRefDto> dtoList = objRefs.stream().map(ObjRef::toDto).toList();
        return Res.ok(dtoList);
    }

    @Operation(summary = "添加对象引用")
    @PostMapping
    public Res<Void> addRef(@RequestBody ObjRefDto objRefDto) {
        objRefService.add(objRefDto);
        return Res.ok();
    }

    @Operation(summary = "删除对象引用")
    @GetMapping("/delete/{id}")
    public Res<Void> removeRef(@PathVariable String id) {
        objRefService.deleteById(id);
        return Res.ok();
    }
}
